﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace PacMan
{
    /// <summary>
    /// Provides the logic for main character named Pacman.
    /// </summary>
    class Pacman : Character, IMainCharacter
    {

        #region Private Fields
        private int _hp = 3;
        private bool _isAlive;
        private int _points = 0;
        #endregion

        #region Public Constructors
        public Pacman(int x, int y, int speed)
            : base(x, y, speed)
        {
            this.Width = 35;
            this.Height = 35;
            IsAlive = true;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the Pacman hit points.
        /// </summary>
        public int HP 
        {
            get 
            {
                return _hp;
            }
            set 
            {
                _hp = value;
            }
        }

        /// <summary>
        /// Gets or sets the current state of Pacman.
        /// </summary>
        public bool IsAlive 
        { 
            get 
            {
                return _isAlive;
            }
            set 
            {
                _isAlive = value;
            }
        }

        /// <summary>
        /// Gets or sets the points, collected by Pacman
        /// </summary>
        public int Points 
        {
            get
            {
                return _points;
            }
            set
            {
                _points = value;
            }
        }

        #endregion
        
        #region Public Methods

        /// <summary>
        /// Gets the key which was pressed and defines the position where should Pacman move.
        /// </summary>
        /// <param name="e"></param>
        public void WhereToMove(KeyEventArgs e)
        {
                
            
                if (e.KeyCode == Keys.Space) 
                {
                    Position = Position.Stay;
                    return;
                }
    
                if (e.KeyCode == Keys.Down)
                {
                    Position = Position.Down;
                    return;
                }
                
                if (e.KeyCode == Keys.Right)
                {
                    Position = Position.Right;                    
                    return;
                }
                
                if (e.KeyCode == Keys.Left)
                {
                    Position = Position.Left;
                    return;
                }
                
                if (e.KeyCode == Keys.Up)
                {
                    Position = Position.Up;
                    return;
                }    
        }

        public void IntersectsWithObstacle(RectangleF[] maze) 
        {
            for (int i = 0; i < maze.Length; i++)
            {
                if (Obstacles(maze[i]) == true)
                {
                    if (Position == Position.Left)
                    {
                        Position = Position.Right;
                        Move();
                        Position = Position.Stay;
                        break;
                    }
                    if (Position == Position.Up)
                    {

                        Position = Position.Down;
                        Move();
                        Position = Position.Stay;
                        break;
                    }
                    if (Position == Position.Right)
                    {

                        Position = Position.Left;
                        Move();
                        Position = Position.Stay;
                        break;
                    }
                    if (Position == Position.Down)
                    {
                        Position = Position.Up;
                        Move();
                        Position = Position.Stay;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Identifies the intersection of Pacman and pacdot.
        /// </summary>
        /// <param name="pacDot"></param>
        /// <returns></returns>
        public bool CollectPacDot(RectangleF pacDot)
        {

            if (X + Width < pacDot.X || pacDot.X + pacDot.Width < X || Y + Height < pacDot.Y || pacDot.Y + pacDot.Height < Y)
            {
                return false;
            }
            else
            {
                _points++;
                return true;
            }
        }

        /// <summary>
        /// Makes pacman to move over maze.
        /// </summary>
        public void Move()
        {
            
            if (Position == Position.Stay) 
            {
                return;
            }
            
            if (Position == Position.Down)
            {
                Y += Speed;
                return;
            }
            if (Position == Position.Right)
            {
                X += Speed;
                if (X >= Environment.MaxX - Width) 
                {
                    X = Environment.MinX;
                }
                return;
            }
            if (Position == Position.Up)
            {
                Y -= Speed;
                return;
            }
            if (Position == Position.Left)
            {
                X -= Speed;
                if (X < Environment.MinX)
                {
                    X = Environment.MaxX - Width;
                }
                return;
            }
        }

        /// <summary>
        /// Identifies the intersection of Pacman and game objects.
        /// </summary>
        /// <param name="gameobj"></param>
        /// <returns></returns>
        public bool IntersectsWithGameObj(GameObject gameobj)
        {
            if (X + Width < gameobj.X || gameobj.X + gameobj.Width < X || Y + Height < gameobj.Y || gameobj.Y + gameobj.Height < Y)
            {
                return false;
            }
            else
            {
                if (gameobj is Character) 
                {
                    _hp--;
                    IsAlive = false;
                }

                if (gameobj is Bonus) 
                {
                    Bonus bonus = (Bonus)gameobj;
                    _points += bonus.Points;
                }
                return true;
            }
        }
        #endregion
    }
}
